<template>
  <ele-pro-table  :toolbar="false" ref="table" :columns="columns" :datasource="url" :where="where">


    <!--工单编号-->
    <template slot="wo_number" slot-scope="{row}">
          <span style="cursor: pointer;color: var(--color-primary)"
                @click="$router.push('/workTickets/workTickets/info?id=' + row.id)">
            {{ row.wo_number }}
          </span>
    </template>

    <!--创建方式-->
    <template slot="createType">
      <span>客服创建</span>
    </template>
    <!--派单人-->
    <template slot="dispatch" slot-scope="{row}">
      <span>{{ row.logs.length >= 2 ? row.logs[1].creator : '' }}</span>
    </template>

    <!--创建方式-->
    <template slot="createType">
      <span>客户创建</span>
    </template>
    <!--产品-->
    <template slot="products" slot-scope="{row}">
      {{ formProducts(row) }}
    </template>
    <!--地址-->
    <template slot="address" slot-scope="{row}">
      <span>{{ row.province_name }} {{ row.city_name }} {{ row.county_name }} {{ row.address }}</span>
    </template>
    <!--负责人-->
    <template slot="master" slot-scope="{row}">
      <span>{{ formMaster(row) }}</span>
    </template>
    <!--协调人-->
    <template slot="synergistic_people" slot-scope="{row}">
      <span>{{ formSynergistic_people(row) }}</span>
    </template>
  </ele-pro-table>
</template>

<script>
const queryFormatter=(query)=>{
  console.log(query,22)
  const newQuery = {}
  newQuery.type = query.work_order_type
  newQuery.user_id = query.user_id
  newQuery.dept_id = query.department
  if (query.date?.length >= 0&&query.type !== "week"&&query.type !== "month") {
    newQuery.date_start = query.date[0]
    newQuery.date_end = query.date[1]
  }
  switch (query.type){
    case "week":
      newQuery.date_type = "week"
      break
    case "month":
      newQuery.date_type = "month"
      break
    case "year":
      newQuery.date_type = "year"
      break
    default:
      newQuery.date_type = "month"
      break
  }
  return newQuery
}
export default {
  props: ["sat","type","query"],
  data() {
    const satOptions = {"非常不满意":1, "不满意":2, "一般":3, "满意":4, "非常满意":5}

    return {
      satOptions: {"非常不满意":1, "不满意":2, "一般":3, "满意":4, "非常满意":5},
      url: '/wos',
      columns: [
        {
          slot: 'wo_number',
          label: '工单编号',
          width: 150,
          align: 'center',
          showOverflowTooltip: true,
        },
        {
          prop: 'type',
          label: '工单类型',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',
        },
        {
          prop: 'customer.name',
          label: '客户',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center'
        },
        {
          prop: 'contact_name',
          label: '联系人',
          align: 'center',
          showOverflowTooltip: true,
          width: 100
        },

        {
          prop: 'contact_phone',
          label: '电话',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          slot: 'address',
          label: '客户地址',
          showOverflowTooltip: true,
          minWidth: 250,
          align: 'center'
        },
        {
          slot: "products",
          label: '产品',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center'
        },
        {
          prop: 'plan_at',
          label: '计划时间',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center'
        },
        {
          slot: 'master',
          label: '负责人',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          slot: 'synergistic_people',
          label: '协同人',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          prop: 'status',
          label: '工单状态',
          minWidth: 160,
          align: 'center',
        },
        {
          prop: 'create_user.nickname',
          label: '创建人',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
        },
        {
          prop: 'create_time_format',
          label: '创建时间',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
        },
        {
          slot: 'dispatch',
          label: '派单人',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
        },
        {
          slot: 'createType',
          label: '创建方式',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',

        },
        // {
        //   columnKey: 'action',
        //   label: '操作',
        //   width: 150,
        //   align: 'center',
        //   resizable: false,
        //   slot: 'action',
        //   fixed: "right"
        // }
      ],
      where: {satisfaction: satOptions[this.sat],...queryFormatter(this.query)}
    }
  },
  watch: {
    sat(current) {
      this.where = {satisfaction: this.satOptions[current],...queryFormatter(this.query)}
      this.$refs.table.reload({where: this.where});
    }
  },
  methods: {
    formSynergistic_people(row) {
      return ((row.users.filter((item) => {
        return item.is_master === 0
      })).map(item => {
        return item.user.realname
      })).join(' ')
    },
    //数据处理
    formProducts(row) {
      return (row.products.map(item => {
        return item?.product?.name
      })).join(' ')
    },
    formMaster(row) {
      if (row.users.filter((item) => {
        return item.is_master === 1
      })) {
        if (((row.users.filter((item) => {
          return item.is_master === 1
        }))[0])) {
          return (((row.users.filter((item) => {
            return item.is_master === 1
          }))[0]).user).realname
        }
      }
    },
  }

}
</script>
